package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 预约设置服务类
 * @author W.J.Y
 */
@Service(version = "1.0.0",interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{

    @Resource
    private OrderSettingDao orderSettingDao;
    @Resource
    private OrderSettingListDao orderSettingListDao;
    /**
     * 根据上传的预约设置文件新增预约设置
     * @param orderSettings     预约设置实例
     * @throws BusinessException    可能出现的业务异常
     */
    @Override
    public void add(List<OrderSetting> orderSettings) throws BusinessException {
        if (orderSettings==null||orderSettings.size()==0){
            throw new BusinessException("文件解析失败，或者无数据");
        }
        for (OrderSetting orderSetting : orderSettings) {
            //首先判断日期是否已存在预约设置，如果已经存在，则为更新
            Integer id = orderSettingDao.findByDate(orderSetting.getOrderDate());
            if (id!=null){
                //编辑
                orderSetting.setId(id);
                orderSettingDao.update(orderSetting);
            }else {
                //新增
                orderSettingDao.add(orderSetting);
            }
        }
    }


    /**
     * 根据传入的开始日期查找所在月的预约设置信息
     * @param dateStr 开始日期
     * @return  预约设置信息
     */
    @Override
    public List<Map> findByDateInterval(String dateStr) {
        String yearAndMonth = StringUtils.substringBeforeLast(dateStr,"-");
        //开始日期与结束日期
        String startDate = yearAndMonth + "-01";
        String endDate = yearAndMonth + "-31";
        //调用查询
        List<OrderSetting> orderSettings = orderSettingDao.findByDateInterval(startDate, endDate);
        List<Map> list= new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map map = new HashMap();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            list.add(map);
        }
        return list;
    }

    /**
     * 编辑预约设置
     * @param orderSetting  预约设置实例
     */
    @Override
    public void update(OrderSetting orderSetting) throws ParseException {
        Date orderDate = orderSetting.getOrderDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(orderDate);
        Date parse = sdf.parse(format);
        Integer id = orderSettingDao.findByDate(parse);
        if (id!=null){
            orderSetting.setId(id);
            orderSettingDao.update(orderSetting);
        }else {
            orderSetting.setReservations(0);
            orderSettingDao.add(orderSetting);
        }


    }
}
